<template>
  <div>
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      v-dialogDrag
      :visible.sync="visible">
      <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading"
               :class="method==='view'?'readonly':''" :disabled="method==='view'"
               label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="病人姓名" prop="patientId"
                          :rules="[{required: true, message: '病人不能为空'}]">
              <patient-select :limit='1' :value="inputForm.patientId"
                              @getValue='getVlaue'></patient-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="就诊记录" prop="visitId"
                          :rules="[
                            {required: true, message: '就诊记录不能为空', trigger: 'change'}
                 ]">
              <el-select v-model="inputForm.visitId" placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in finalVisitList"
                  :key="item.id"
                  :label="item.visitRecord"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消费项" prop="consumptionItem"
                          :rules="[
                             {required: true, message: '消费项不能为空', trigger: 'change'}
                 ]">
              <el-select v-model="inputForm.consumptionItem" placeholder="请选择" style="width: 100%;"
                         @change="selectDrug">
                <el-option
                  v-for="item in drugList"
                  :key="item.id"
                  :label="item.drugName"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单价" prop="unitPrice"
                          :rules="[
                            {required: true, message: '消费项不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.unitPrice" placeholder="请填写单价"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数量" prop="amount"
                          :rules="[
                            {required: true, message: '数量不能为空', trigger: 'blur'}
                 ]">
              <el-input-number @change="amountChange" v-model="inputForm.amount" :step="1" placeholder="请填写数量"
                               style="width: 100%;"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="费用金额" prop="cost"
                          :rules="[
                            {required: true, message: '费用金额不能为空', trigger: 'blur'}
                 ]">
              <el-input v-model="inputForm.cost" placeholder="请填写费用金额"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式" prop="paymentMethod"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.paymentMethod" placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('payment_method')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付状态" prop="paymentStatus"
                          :rules="[
                 ]">
              <el-select v-model="inputForm.paymentStatus" placeholder="请选择" style="width: 100%;">
                <el-option
                  v-for="item in $dictUtils.getDictList('application_state')"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  </div>
</template>

<script>
  import billingInfoService from '@/api/billing/billingInfoService'
  import patientSelect from '@/components/patientSelect'
  import visitInfoService from '@/api/visit/visitInfoService'
  import drugInfoService from '@/api/drug/drugInfoService'

  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          patientId: '',
          visitId: '',
          consumptionItem: '',
          unitPrice: '',
          amount: '',
          cost: '',
          paymentMethod: '',
          paymentStatus: ''
        },
        visitList: [],
        drugList: [],
        finalVisitList: []
      }
    },
    components: {
      patientSelect
    },
    methods: {
      init (method, id) {
        this.listVisit()
        this.listDrug()
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建结算信息`
        } else if (method === 'edit') {
          this.title = '修改结算信息'
        } else if (method === 'view') {
          this.title = '查看结算信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            billingInfoService.queryById(this.inputForm.id).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            billingInfoService.save(this.inputForm).then(({data}) => {
              this.visible = false
              this.$message.success(data)
              this.$emit('refreshDataList')
              this.loading = false
            }).catch(() => {
              this.loading = false
            })
          }
        })
      },
      listDrug () {
        drugInfoService.listAll(this.drugSearchForm).then(res => {
          this.drugList = res.data
        })
      },
      listVisit () {
        visitInfoService.listAll(this.visitSearchForm).then(res => {
          this.visitList = res.data
        })
      },
      selectDrug (value) {
        let frug = this.drugList.find(item => item.id === value)
        this.inputForm.unitPrice = frug.unitPrice
        this.inputForm.dosage = frug.oralDose
      },
      amountChange (value) {
        this.inputForm.cost = this.inputForm.unitPrice * value
      },
      getVlaue (id) {
        this.inputForm.patientId = id
        this.finalVisitList = this.visitList.filter(item => item.patientId === id)
      }
    }
  }
</script>


